<template>
  <div class="detail">
    <div class="detail-left" v-if="detail">
      <div class="shopimg">
        <img :src="detail.s_goods_photos[img_idx].path" alt="" />
      </div>
      <div class="shopimg-bottom">
        <div class="shop-btm-img" v-for="(el,idx) in detail.s_goods_photos" :key="idx" @click="select_img(idx)">
          <img :src="el.path" alt="">
        </div>
      </div>
    </div>
    <div class="detail-right">
      <div class="dtr-top">
        <div>
          <h1>{{ detail.name }}</h1>
        </div>
        <div>
          <h4>商品描述:</h4>
          {{ detail.desc }}
        </div>
        <div>
          <h4>价格:{{ detail.price }}</h4>
        </div>
        <div>商品支持： 花呗分期3/6/12期 以旧换新 门店送货上门</div>
        <div>
          <el-input-number
            v-model="num"
            controls-position="right"
            :min="1"
            :max="detail.stock_num"
          ></el-input-number>
        </div>
        <el-button type="danger" :plain="true" @click.native="addshopcar"
          >加入购物车</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      detail: "",
      num: 1,
      img_idx:0
    };
  },
  watch:{
    //监听路由,重新渲染页面
    $route:{
      handler: async function(){
        await this.$store.dispatch("goodDetail", this.$route.params.id);
        this.detail = this.$store.state.detail;
        // console.log(2);
      }
    }
  },
  async created() {
    //    console.log(this.$route.query);
    //请求商品详情信息
    await this.$store.dispatch("goodDetail", this.$route.params.id);
    this.detail = this.$store.state.detail;
    console.log(this.$route);
  },
  methods: {
     //加入购物车
    addshopcar() {
      this.$store.dispatch("addshoppingCart", {
        num: this.num,
        goods_id: this.$route.params.id,
      }).then(()=>{
          this.$message({
        showClose: true,
        message: "成功加入购物车",
        type: 'success'
      });
      });
    },
    select_img(idx){
      this.img_idx=idx
    }
  },
};
</script>

<style lang="scss" scoped>
.detail {
  border-top: 1px solid rgb(224, 224, 224);
  padding: 20px;
  padding-top: 32px;
  display: flex;
  margin-top: 80px;
  justify-content: center;
  .detail-left {
    // border: 1px solid green;
    // display: flex;
    flex-wrap: wrap;
    width: 800px;
    justify-content: space-around;
    .shopimg {
      width: 560px;
      height: 560px;
      margin-bottom: 30px;
      // border: 1px solid black;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .shopimg-bottom{
      display: flex;
      .shop-btm-img{
      margin-right: 15px;
        width: 100px;
        height: 100px;
        border: 1px solid rgb(221, 220, 220);
        cursor: pointer;
        &:hover{
          border: 1px solid black;
        }
        img{
          width: 100%;
          height: 100%;
        }
      }
    }
  }
  .detail-right {
    width: 400px;
    // border: 1px solid skyblue;
    display: flex;
    flex-wrap: wrap;
    .dtr-top {
      width: 100%;
      div {
        margin-bottom: 20px;
      }
    }
  }
}
</style>